package com.base.cn.platform.os.controller.classes.topic;

import com.base.cn.platform.os.common.classes.topic.ClassesTopicClassifyEnum;
import com.base.cn.platform.os.common.controller.BaseController;
import com.base.cn.platform.os.common.mybatis.Pagination;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.common.utils.action.ActionKey;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.entity.classes.Classes;
import com.base.cn.platform.os.entity.classes.topic.ClassesTopic;
import com.base.cn.platform.os.service.classes.ClassesBiz;
import com.base.cn.platform.os.service.classes.member.ClassesMemberBiz;
import com.base.cn.platform.os.service.classes.topic.ClassesTopicBiz;
import com.base.cn.platform.os.service.manage.user.integral.CusUserIntegralService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 班级话题
 *
 * @author SK
 * @since 2018-05-18
 */
@RestController
@RequestMapping("/course")
public class ClassesTopicController extends BaseController {

    @Autowired
    private ClassesTopicBiz classesTopicBiz;
    @Autowired
    private ClassesMemberBiz classesMemberBiz;
    @Autowired
    private ClassesBiz classesBiz;
    @Autowired
    private CusUserIntegralService cusUserIntegralService;

    @RequestMapping("/classesTopicClassify")
    public Map<Integer, String> classesTopicClassify() {
        return ClassesTopicClassifyEnum.valueMap();
    }

    /**
     * 根据id查询班级话题
     *
     * @param id               班级话题id
     * @param setCreator       设置创建人
     * @param setClasses       设置班级
     * @param setTopicType     设置话题类型
     * @param updateStatistics 更新话题统计数据
     * @return 班级话题
     */
    @RequestMapping("/findClassesTopicById")
    public ClassesTopic findClassesTopicById(@RequestParam("id") BigDecimal id,
                                             @RequestParam("setCreator") boolean setCreator,
                                             @RequestParam("setClasses") boolean setClasses,
                                             @RequestParam("setTopicType") boolean setTopicType,
                                             @RequestParam("updateStatistics") boolean updateStatistics) {
        return classesTopicBiz.findClassesTopicById(id, setCreator, setClasses, setTopicType, updateStatistics);
    }

    /**
     * 根据id查询班级话题
     *
     * @param ids          班级话题id
     * @param setCreator   设置创建人
     * @param setClasses   设置班级
     * @param setTopicType 设置类型
     * @return 班级话题 key:班级话题id value:班级话题
     */
    @RequestMapping("/findClassesTopicByIds")
    public Map<BigDecimal, ClassesTopic> findClassesTopicByIds(@RequestParam("ids") String ids,
                                                               @RequestParam("setCreator") boolean setCreator,
                                                               @RequestParam("setClasses") boolean setClasses,
                                                               @RequestParam("setTopicType") boolean setTopicType) {
        return classesTopicBiz.findClassesTopicByIds(ids, setCreator, setClasses, setTopicType);
    }

    /**
     * 查询班级话题列表
     *
     * @param classesTopic 班级话题查询条件
     * @param count        数量
     * @param order        排序 1.最新 2.最热
     * @param setCreator   设置创建人
     * @param setClasses   设置班级
     * @param setTopicType 设置话题类型
     * @return 班级话题列表
     */
    @RequestMapping("/findClassesTopicList")
    public List<ClassesTopic> findClassesTopicList(@RequestBody ClassesTopic classesTopic,
                                                   @RequestParam(value = "count", required = false) Integer count,
                                                   @RequestParam(value = "order", required = false) Integer order,
                                                   @RequestParam("setCreator") boolean setCreator,
                                                   @RequestParam("setClasses") boolean setClasses,
                                                   @RequestParam("setTopicType") boolean setTopicType) {
        return classesTopicBiz.findClassesTopicList(classesTopic, count, order, setCreator, setClasses, setTopicType);
    }

    /**
     * 查询班级话题列表
     *
     * @param classesTopic 班级话题查询条件
     * @param currentPage  当前页
     * @param pageSize     每页数据条数
     * @param order        排序 1.最新 2.最热
     * @param setCreator   设置创建人
     * @param setClasses   设置班级
     * @param setTopicType 设置话题类型
     * @return 班级话题列表
     */
    @RequestMapping("/findClassesTopicListPage")
    public PageInfo<ClassesTopic> findClassesTopicListPage(@RequestBody ClassesTopic classesTopic,
                                                           @RequestParam("currentPage") int currentPage,
                                                           @RequestParam(value = "pageSize", required = false) Integer pageSize,
                                                           @RequestParam(value = "order", required = false) Integer order,
                                                           @RequestParam("setCreator") boolean setCreator,
                                                           @RequestParam("setClasses") boolean setClasses,
                                                           @RequestParam("setTopicType") boolean setTopicType) {
        Pagination pagination = new Pagination();
        if (DataUtil.isPositive(pageSize)) {
            pagination.setPageSize(pageSize);
        }
        pagination.setCurrentPage(currentPage);
        return classesTopicBiz.findClassesTopicListPage(classesTopic, pagination, order, setCreator, setClasses, setTopicType);
    }

    /**
     * 查询班级话题列表
     *
     * @param userId       前台用户id
     * @param currentPage  当前页
     * @param pageSize     每页数据量
     * @param which        1.全部话题 2.发表的话题 3.参与的话题 4.转发的话题
     * @param setCreator   设置创建人
     * @param setClasses   设置班级
     * @param setTopicType 设置话题类型
     * @return 班级话题列表
     */
    @RequestMapping("/findClassesPersonalTopicPage")
    public PageInfo<ClassesTopic> findClassesPersonalTopicPage(@RequestParam("userId") BigDecimal userId,
                                                               @RequestParam("currentPage") int currentPage,
                                                               @RequestParam(value = "pageSize", required = false) Integer pageSize,
                                                               @RequestParam(value = "which", required = false, defaultValue = "1") Integer which,
                                                               @RequestParam("setCreator") boolean setCreator,
                                                               @RequestParam("setClasses") boolean setClasses,
                                                               @RequestParam("setTopicType") boolean setTopicType) {
        Pagination page = new Pagination();
        page.setCurrentPage(currentPage);
        if (DataUtil.isPositive(pageSize)) {
            page.setPageSize(pageSize);
        }
        return classesTopicBiz.findClassesPersonalTopicPage(userId, page, which, setCreator, setClasses, setTopicType);
    }

    /**
     * 我的话题统计
     *
     * @param userId 前台用户id
     * @return 我的话题统计
     */
    @RequestMapping("/classesPersonalTopicStatistics")
    public Map<String, Object> classesPersonalTopicStatistics(@RequestParam("userId") BigDecimal userId) {
        return classesTopicBiz.classesPersonalTopicStatistics(userId);
    }

    /**
     * 保存班级话题
     *
     * @param classesTopic 班级话题
     * @param classesIds   第N步
     * @return 保存结果
     */
    @RequestMapping("/saveClassesTopic")
    @ResponseBody
    public Map<String, Object> saveClassesTopic(@RequestBody ClassesTopic classesTopic,
                                                @RequestParam("classesIds") String classesIds) {
        Map<String, Object> check = this.checkClassesTopic(classesTopic, classesIds);
        if (check != null) {
            return check;
        }
        classesTopic = classesTopicBiz.saveClassesTopic(classesTopic, classesIds);
        // 发表话题获取积分
        cusUserIntegralService.addUserIntegral(ActionKey.TOPIC,classesTopic.getCreateUserId(),null,0,2);
        return ResultUtil.SUCCESS("保存班级话题成功", classesTopic);
    }

    /**
     * 更新班级话题置顶
     *
     * @param ids 班级话题ids
     * @param top 置顶 1.是 2.否
     * @return 操作结果
     */
    @RequestMapping("/updateClassesTopicTop")
    public Map<String, Object> updateClassesTopicTop(@RequestParam("ids") String ids,
                                                     @RequestParam("top") int top) {
        ids = DataUtil.filterNotNumberStr(ids);
        if (StringUtils.isEmpty(ids)) {
            return ResultUtil.ERROR("请选择操作的班级话题");
        }
        classesTopicBiz.updateClassesTopicTop(ids, top);
        return ResultUtil.SUCCESS("更新班级话题置顶状态成功");
    }

    /**
     * 更新班级话题状态
     *
     * @param ids    班级话题id
     * @param status 班级话题状态 0.未发布 1.已发布 2.已禁用 3.已删除
     * @return 操作结果
     */
    @RequestMapping("/updateClassesTopicStatus")
    public Map<String, Object> updateClassesTopicStatus(@RequestParam("ids") String ids,
                                                        @RequestParam("status") int status) {
        ids = DataUtil.filterNotNumberStr(ids);
        if (StringUtils.isEmpty(ids)) {
            return ResultUtil.ERROR("请选择操作的班级话题");
        }
        classesTopicBiz.updateClassesTopicStatus(ids, status);
        return ResultUtil.SUCCESS("更新班级话题状态成功");
    }

    /**
     * 检查班级话题数据
     *
     * @param classesTopic 班级话题
     */
    private Map<String, Object> checkClassesTopic(ClassesTopic classesTopic, String classesIds) {
        if (StringUtils.lengthOutOfRange(classesTopic.getTitle(), 1, 50)) {
            return ResultUtil.ERROR("话题标题在1~50个字符之间");
        }
        if (StringUtils.isEmpty(StringUtils.deleteHtml(classesTopic.getContent()))) {
            return ResultUtil.ERROR("填写话题内容");
        }
        if (!DataUtil.isPositive(classesTopic.getClassify())) {
            return ResultUtil.ERROR("选择话题类型");
        }
        classesIds = DataUtil.filterNotNumberStr(classesIds);
        if (StringUtils.isEmpty(classesIds)) {
            return ResultUtil.ERROR("选择至少一个班级");
        }
        if(classesTopic.getSource() == 1){
            BigDecimal userId = classesTopic.getCreateUserId();
            // 班级状态判断
            Classes classes = classesBiz.findById(new BigDecimal(classesIds));
            if (classes.getStatus() != 1) {
                return ResultUtil.ERROR("班级已停班，禁止发表话题");
            }
            // 是否是本班成员
            boolean isJoined = classesMemberBiz.isJoined(new BigDecimal(classesIds),userId);
            if(!isJoined){
                return ResultUtil.ERROR("非本班成员，禁止发表话题");
            }
            // 是否已被本班封禁
            boolean isBanned = classesMemberBiz.isBanned(new BigDecimal(classesIds),userId);
            if(!isBanned){
                return ResultUtil.ERROR("你被禁止在本班发表话题");
            }
        }
        return null;
    }

    /**
     * 查询话题数量
     * @param classesTopic 查询条件
     * @return  BigDecimal
     */
    @RequestMapping("/getClassTopicCount")
    public BigDecimal getClassTopicCount(@RequestBody ClassesTopic classesTopic){
        return classesTopicBiz.getClassTopicCount(classesTopic);
    }

}
